# use the official Bun image
# see all versions at https://hub.docker.com/r/oven/bun/tags
FROM oven/bun:1 AS base
WORKDIR /usr/src/app

# install dependencies into temp directory
FROM base AS install
RUN mkdir -p /temp/package
COPY package.json bun.lock /temp/package/
COPY scripts /temp/package/scripts
RUN cd /temp/package && bun install --frozen-lockfile --production

# copy node_modules from temp directory
# then copy all (non-ignored) project files into the image
FROM base AS build
COPY --from=install /temp/package/node_modules node_modules
COPY . .

# build the app
RUN bun run build

# copy production code to final image
FROM base AS release
COPY --from=install /temp/package/node_modules node_modules
COPY --from=build /usr/src/app/dist/index.js index.js
COPY --from=build /usr/src/app/public ./public

# run the app
EXPOSE 3000 9090
ENTRYPOINT [ "bun", "run", "index.js" ]
